package com.wang.admin.comtroller;


import com.wang.SysMenu;
import com.wang.admin.entity.LoginBody;
import com.wang.admin.entity.SysUser;
import com.wang.admin.service.MenuService;
import com.wang.admin.service.PermissionService;
import com.wang.admin.service.UserService;
import com.wang.commom.constant.Constants;
import com.wang.commom.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Author: tiger
 * @Data: 2024/10/14 21:36
 * @Description: 登录验证
 **/

@RestController
//@RequestMapping("/dev-api")
public class SysLoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/login")  //验证谁登录都可以 - TODO
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = userService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode()
                , loginBody.getUuid());

        //ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        //SysUser user = SecurityUtils.getLoginUser().getUser();

        SysUser user = new SysUser();
        user.setUserName("admin");
        user.setPassword("123456");
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }


    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        //Long userId = SecurityUtils.getUserId();

        Long userId = 1L;
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    @RequestMapping("/logout")
    public AjaxResult logOut(){
        return AjaxResult.success("退出成功");
    }
}
